package cn.taketoday.live.controller;

import org.springframework.stereotype.Controller;

import java.io.IOException;
import java.util.concurrent.CopyOnWriteArraySet;

import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;

import lombok.extern.slf4j.Slf4j;

/**
 * @author BanqiJane
 */
@Slf4j
@Controller
@ServerEndpoint("/danmu/sub")
public class DanmuWebsocket {
  private static final CopyOnWriteArraySet<DanmuWebsocket> webSocketServers = new CopyOnWriteArraySet<>();
  private Session session;

  @OnOpen
  public void onOpen(Session session) {
    this.session = session;
    webSocketServers.add(this);
    log.info("DanmuWebsocket open");
  }

  @OnClose
  public void onClose() {
    webSocketServers.remove(this);
  }

  @OnMessage
  public void onMessage(String message) throws IOException {
    for (DanmuWebsocket danmuWebsocket : webSocketServers) {
      danmuWebsocket.session.getBasicRemote().sendText(message);
    }
  }

  @OnError
  public void onError(Session session, Throwable error) {
    log.error("DanmuWebsocket error", error);
  }

  public void sendMessage(String message) throws IOException {
    for (DanmuWebsocket danmuWebsocket : webSocketServers) {
      synchronized(danmuWebsocket.session) {
        danmuWebsocket.session.getBasicRemote().sendText(message);
      }
    }
  }

  public Session getSession() {
    return session;
  }

  public void setSession(Session session) {
    this.session = session;
  }

}
